package com.hisysteTopet.hisystepet.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hisysteTopet.hisystepet.bean.*;
import com.hisysteTopet.hisystepet.interceptors.JWTInterceptor;
import com.hisysteTopet.hisystepet.mapper.PetLifeGoodsMapper;
import com.hisysteTopet.hisystepet.mapper.PetMapper;
import com.hisysteTopet.hisystepet.mapper.UserMessageMapper;
import com.hisysteTopet.hisystepet.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
public class UserMessageController {

    @Autowired
     User user;

    @Autowired
    UserService userService;

    @Autowired
    private UserMessage userMessage;

    @Autowired
    private Msg msg;

    @Autowired
    LoginController loginController;

    @Autowired
    private UserMessageMapper userMessageMapper;

    @Autowired
    private UserMessageService userMessageService;



    @Autowired
    private FosterService fosterService;

    @Autowired
    JWTInterceptor jwtInterceptor;

    @Autowired
    private IndentToPetService indentService;

    @Autowired
    private IndentToPetLifeGoodsService indentToPetLifeGoodsService;

    @Autowired
    private Pet pet;

    @Autowired
    private PetLifeGoods petLifeGoods;

    @Autowired
    private PetMapper petMapper;


    @Autowired
    private  PetLifeGoodsService petLifeGoodsService;

    @Autowired
    private PetLifeGoodsMapper petLifeGoodsMapper;

    @Autowired
    private ShopCartService shopCartService;

    @Autowired
    private ShopCart shopCart;

    @Autowired
    private PetService petService;

    // 我的信息
    @RequestMapping("/userMessage")
    @ResponseBody
    public Msg userMessage(){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null){
           userMessage = userMessageService.userMessage(username);
           msg = Msg.success().add("userMessage",userMessage);
       }else {
           msg = Msg.fail();
       }

       return msg;
    }

    // 我的宠物
    @RequestMapping("/userAndPet")
    @ResponseBody
    public Msg UserToPet(){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        System.out.println("用户名为:"+username);
        if (tokenState && username != null){

            List<UserAndPet> userAndPet = indentService.selectIntendToPet(username);
            msg = Msg.success().add("userAndPet",userAndPet);
        }else {
            msg = Msg.fail();
        }

        return msg;
    }


    // 我的寄养信息
    @RequestMapping("/userToFoster")
    @ResponseBody
    public Msg UserToFoster(){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null){
            List<Foster> userToFoster = fosterService.selectByUsername(username);
            msg = Msg.success().add("userToFoster",userToFoster);
        }else {
            msg = Msg.fail();
        }

        return msg;
    }

    // 删除寄养信息
    @RequestMapping(value = "/userToDeleteFoster",method = RequestMethod.DELETE)
    @ResponseBody
    public Msg UserToDeleteFoster(String fosterId){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null){
            fosterService.deleteByFosterId(fosterId,username);
        }else {
            msg = Msg.fail();
        }
        return msg;
    }


    // 我的挂号信息
    @RequestMapping(value = "/userRegistration")
    @ResponseBody
    public Msg UserRegistration(){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null){
            msg = Msg.success();
        }else {
            msg = Msg.fail();
        }
        return msg;
    }

    // 挂号信息
    @RequestMapping(value = "/registration" ,method = RequestMethod.POST)
    @ResponseBody
    public Msg Registration(String why,String data){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        log.info("传来的信息{}",why,data,username);
        if (tokenState && username != null){
            log.info("asd{}",data);
            fosterService.insertByFosterId(username,why,data);
            msg = Msg.success();
        }else {
            msg = Msg.fail();
        }
        return msg;
    }

    // 查看订单信息
    @RequestMapping(value = "/indentMessage")
    @ResponseBody
    public Msg selectAllIndentToPetByUsername(){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null){
            List<IndentPet> indentList = indentService.selectAllIndentByUsername(username);
            msg = Msg.success().add("list",indentList);
        }else {
            msg = Msg.fail();
        }
        return msg;
    }

    // 修改密码和收获地址
    @RequestMapping(value = "/userUpdatePasswordAndAddress",method = RequestMethod.PUT)
    @ResponseBody
    public Msg userUpdatePasswordAndAddress(String oldPassword, String newPassword,
                                            String address){
        int i = 0;
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null){
           user = userService.selectToMessage(username);


            if (oldPassword.equals(newPassword)){
                msg = Msg.fail().add("updateToNew", "修改的密码和原本的密码一样");
            }else{
                if (oldPassword.equals("")||newPassword.equals("")){
                    i = -1;
                }

                if (oldPassword.equals(user.getPassword()) && i==0){
                    if (newPassword.equals(user.getPassword())){
                        msg = Msg.fail().add("updateToOldAndNew", "修改的密码不能和原本密码一样");
                    }else {
                        userService.updateToPassword(username,newPassword);
                        i = 0;
                        msg = Msg.success().add("password","修改密码成功");
                    }
                }else {
                    msg = Msg.fail().add("updateToOld", "输入的旧密码错误");
                    if (oldPassword.equals("")){
                        msg = Msg.fail().add("oldPassword", "旧密码不能为空");
                    }

                    if (newPassword.equals("")){
                        msg = Msg.fail().add("newPassword", "新密码不能为空");
                    }
                }
            }
        }else {
            msg = Msg.fail().add("password","修改密码失败");
        }

        if (!address.equals("")){
                userMessageService.updateToAddress(username,address);
                msg = Msg.success().add("address","修改地址成功");
        }


        return msg;
    }


    // 宠物的分页操作
    @RequestMapping(value = "/petPaging",method = RequestMethod.GET)
    @ResponseBody
    public Msg getPetPage( int currentPage){
        String username = JWTInterceptor.username;
            Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null){
            Page page = new Page(currentPage,4);
            msg = Msg.success().add("petList",petMapper.selectPage(page,null));
        }else {
            msg = Msg.fail();
        }

        return msg;
    }

    // 添加宠物订单
    @RequestMapping(value = "/insertIndentToPet",method = RequestMethod.GET)
    @ResponseBody
    public Msg insertIndentToPet(double sum_money,String address,Integer petId,
                                 Integer petNum,String petName){
//        log.info("sum_money，String address{}",sum_money,address);
        System.out.println("宠物订单信息"+sum_money+" "+address+" "+petId+" "+petNum);
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Date data = Timestamp.valueOf(df.format(new Date()));// new Date()为获取当前系统时间
        if (tokenState && username != null){
            // 判断库存
            pet = petMapper.selectToPetId(petId);
            if (pet.getPetNum()>=petNum){
                indentService.insertIntend(sum_money, data, address,username,petId,petNum,petName);
                petMapper.updateToPetNum(petNum,petId);
                msg = Msg.success();
            }else {
                msg = Msg.fail();
            }
        }else {

        }
        return msg;
    }


    // 宠物生活用品的分页操作
    @RequestMapping(value = "/petLifeGoodsPaging",method = RequestMethod.GET)
    @ResponseBody
    public Msg getLifeGoodsPage( int currentPage){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null){
            Page page = new Page(currentPage,2);
            msg = Msg.success().add("petGoodsList",petLifeGoodsMapper.selectPage(page,null));
        }else {
            msg = Msg.fail();
        }

        return msg;
    }


    // 添加宠物生活用品订单
    @RequestMapping(value = "/insertIndentToPetLifeGoods",method = RequestMethod.GET)
    @ResponseBody
    public Msg insertIndentToPetLifeGoods(double sum_money,String address,Integer priceId,
                                 Integer priceNum,String priceName){
//        log.info("sum_money，String address{}",sum_money,address);
//        System.out.println("宠物订单信息"+sum_money+" "+address+" "+priceId+" "+petNum);
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Date data = Timestamp.valueOf(df.format(new Date()));// new Date()为获取当前系统时间
        if (tokenState && username != null){
            // 判断库存
            petLifeGoods = petLifeGoodsService.selectToPetId(priceId);
            if (petLifeGoods.getPriceNum()>=priceNum){
                indentToPetLifeGoodsService.insertIntend(sum_money, data, address,username,priceId,priceNum,priceName);
//                petMapper.updateToPetNum(petNum,priceId);
                petLifeGoodsService.updateToPriceNum(priceNum,priceId);
                msg = Msg.success();
            }else {
                msg = Msg.fail();
            }
        }else {

        }
        return msg;
    }

    // 查看所有宠物生活用品订单
    @RequestMapping(value = "/indentToGoodsMessage")
    @ResponseBody
    public Msg selectAllIndentToPetLifeGoodsByUsername(){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null){
            List<IndentPetLifeGoods> indentList = indentToPetLifeGoodsService.selectAllIndentByUsername(username);
            msg = Msg.success().add("list",indentList);
        }else {
            msg = Msg.fail();
        }
        return msg;
    }



    // 注册用户
    @RequestMapping(value = "/userRegistered",method = RequestMethod.GET)
    @ResponseBody
    public Msg userRegistered(String userId,String password,String username,String sex,Long phone){
            user = userService.selectToMessage(userId);
            log.info("用户信息{}",user);
            if (user == null){
                userService.insertToUser(userId,password);
                userMessageService.insertUserMessage(userId,username,sex,phone,null);
                msg = Msg.success();
            }else {
                msg = Msg.fail();
                }
        return msg;
    }


    // 添加购物车信息
    @RequestMapping(value = "/insertShowCartMessage",method = RequestMethod.POST)
    @ResponseBody
    public Msg insertShowCartMessage(Integer Id,
                                     String classify,String url,
                                     String name,Double money){
//        System.out.println(Id+Species);
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
//        System.out.println("我是nameasdasd"+name);
        if (tokenState && username != null){
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            Date data = Timestamp.valueOf(df.format(new Date()));// new Date()为获取当前系统时间
            if ( !classify.equals(" ")) {
                if (classify.equals("宠物用品")) {
                    shopCart = shopCartService.SelectByUsernameAndLifeGoodsId(username,Id);
                    if (shopCart == null){
                        shopCartService.InsertShopCartMessage(username, null, Id,data,
                                classify,url,null,name,money);
                        msg = Msg.success();
                    }else {
                        msg = Msg.fail();
                    }

                } else if (classify.equals("宠物")) {
                    shopCart = shopCartService.SelectByUsernameAndPetId(username,Id);
                    if (shopCart == null){
                      shopCartService.InsertShopCartMessage(username,Id ,null ,data,
                                classify,url,name,null,money);
                        msg = Msg.success();
                    }else {
                        msg = Msg.fail();
                    }

                } else {
                    msg = Msg.fail();
                }
            }else {
                msg = Msg.fail();
            }
        }else {
            msg = Msg.fail();
        }


        return msg;
    }

    // 获取到购物车的所有信息
    @RequestMapping(value = "/selectAllShopCartByUsername")
    @ResponseBody
    public Msg selectAllShopCartByUsername(){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null){
            List<ShopCart> list = shopCartService.SelectAllShopCartByUsername(username);
            msg = Msg.success().add("list",list);
        }else {
            msg = Msg.fail();
        }
        return msg;
    }

    // 通过ID和物品分类查询物品介绍
    @RequestMapping(value = "/selectMessageByIdAndClassify")
    @ResponseBody
    public Msg selectMessageByIdAndClassify(Integer Id,String Classify){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        log.info("传来的值{}",Id,Classify);
        if (tokenState && username != null){
            if (Classify.equals("宠物用品")){
                petLifeGoods = petLifeGoodsService.selectToPetId(Id);
                msg = Msg.success().add("message",petLifeGoods.getIntroduce());
            }else if (Classify.equals("宠物")){
                pet = petService.selectToPetId(Id);
                msg = Msg.success().add("message",pet.getPetIntroduce());
            }else {
                msg = Msg.fail();
            }
        }else {
            msg = Msg.fail();
        }


        return msg;
    }

    // 通过ID和物品分类删除购物车的信息
    @RequestMapping(value = "/deleteShopCartMessageByIdAndClassify")
    @ResponseBody
    public Msg deleteShopCartMessageByIdAndClassify(Integer Id,String Classify){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        log.info("传来的值{}",Id,Classify);
        if (tokenState && username != null){
            if (Classify.equals("宠物用品")){
                shopCartService.DeleteshouCartMessageByIdAndClassifyToPetLifeGoods(Id,username,Classify);
                msg = Msg.success();
            }else if (Classify.equals("宠物")){
                shopCartService.DeleteshouCartMessageByIdAndClassifyToPet(Id,username,Classify);
                msg = Msg.success();
            }else {
                msg = Msg.fail();
            }
        }else {
            msg = Msg.fail();
        }


        return msg;
    }


}
